#!/bin/bash

set -eu
cd `dirname $0`

TARGET_DIRS=(../wasm/examples ../wasm/launcher ../wasm/mml-studio ../wasm/code-maker)
SRC_REGEX="src=\\\".*/pyxel.js\\\""
LOCAL_SRC="src=\\\"../pyxel.js\\\""
CDN_SRC="src=\\\"https://cdn.jsdelivr.net/gh/kitao/pyxel/wasm/pyxel.js\\\""

if [ "$1" == "local" ]; then
    find "${TARGET_DIRS[@]}" -name '*.html' -type f | xargs sed -i -e "s#$SRC_REGEX#$LOCAL_SRC#"

elif [ "$1" == "cdn" ]; then
    find "${TARGET_DIRS[@]}" -name '*.html' -type f | xargs sed -i -e "s#$SRC_REGEX#$CDN_SRC#"
else
    echo "switch_html_scripts local_or_cdn"
    exit 0
fi

find "${TARGET_DIRS[@]}" -name '*.html-e' -exec rm -f {} +
